package com.jinlejia.erp.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.freedom.yefeng.yfrecyclerview.YfListAdapter;
import com.freedom.yefeng.yfrecyclerview.YfSimpleViewHolder;
import com.jinlejia.erp.R;
import com.jinlejia.erp.databinding.ItemTagsLayoutBinding;

import java.util.ArrayList;


/**
 * 客源标签Adapter
 */
public class TagsAdapter extends YfListAdapter<String> {

    private Context context;
    private ArrayList<String> sDataList = new ArrayList<>();

    public TagsAdapter(ArrayList<String> data) {
        super(data);
    }

    @Override
    public RecyclerView.ViewHolder onCreateDataViewHolder(ViewGroup parent) {
        context = parent.getContext();
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_tags_layout, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateEmptyViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty_layout, parent, false);

        return new YfSimpleViewHolder(view);
    }

    @Override
    public void onBindDataViewHolder(RecyclerView.ViewHolder viewHolder, final int i) {
        ItemTagsLayoutBinding binding = DataBindingUtil.getBinding(viewHolder.itemView);
        final String tag = mData.get(i);
        binding.tag.setText( tag );

        binding.tag.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    sDataList.add(tag);
                }else{
                    sDataList.remove(tag);
                }
            }
        });

        binding.getRoot().setTag(mData.get(i));
    }

    @Override
    public RecyclerView.ViewHolder onCreateLoadingViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.loading_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateFooterViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.footer_loading, parent, false);
        return new YfSimpleViewHolder(view);
    }


    static final class ViewHolder extends RecyclerView.ViewHolder {
        public ViewHolder(View itemView) {
            super(itemView);
            DataBindingUtil.bind(itemView);
        }
    }

    /**
     * 将list拼接成String，以";"分隔
     * @return
     */
    public String getSelectStr(){
        StringBuilder sb = new StringBuilder();
        for( String s:sDataList ){
            sb= sb.append(";").append(s);
        }
        return sb.toString().replaceFirst(";","");
    }

}
